package com.biz.primus.management.cat.controller;

import com.biz.primus.management.base.controller.AbstractBaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Map;

import static com.google.common.collect.Maps.newHashMap;

/**
 * Created by defei on 1/7/17.
 */
@Slf4j
@Controller
public class FileUploadController extends AbstractBaseController {

    @RequestMapping(value = "file/upload", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> forwardToJsp(@RequestParam("file") MultipartFile file) throws IOException {

        log
                .debug("Received file/upload POST request with {} byte {} file:{}.", file.getBytes().length, file.getContentType(), file.getOriginalFilename());
//        String fileName = DateUtils.getDateFormat(new Date(), "yyyyMMddHHmmss_") + file.getOriginalFilename();
        //        OssUtils.put(configCacheAdapter.getConfig(), file.getInputStream(), fileName);
        Map<String, Object> result = newHashMap();
        result.put("success", true);
        //        result.put("url", OssUtils.getDownloadUrl(configCacheAdapter.getConfig(), fileName));
        result.put("url", "http://www.biz-united.com.cn/new-biz/images/code.png");
//        result.put("file", fileName);
        return result;
    }

    @RequestMapping(value = "file/download", method = RequestMethod.GET)
    public ModelAndView forwardToJsp(@RequestParam("file") String file) throws IOException {

        log.debug("Received file/download POST request with param file:{}.", file);
        //        return new ModelAndView("redirect:" + OssUtils.getDownloadUrl(configCacheAdapter.getConfig(), file));
        return new ModelAndView("redirect:http://www.biz-united.com.cn/new-biz/images/code.png");
    }

    //    @Autowired
    //    private ConfigCacheAdapter configCacheAdapter;
}
